package kt.com.bilin.coin.utils

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText

object BLUtils {
    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private const val MIN_CLICK_DELAY_TIME = 300
    private var lastClickTime: Long = 0

    /**
     * 限制重复点击
     *
     * @param minClickDelayTime
     * @return true，没有重复点击；false，minClickDelayTime 内连续点击两次
     */
    @JvmStatic
    fun notRepeatClick() = notRepeatClick(MIN_CLICK_DELAY_TIME)

    /**
     * 限制重复点击
     *
     * @return true，没有重复点击；false，300ms 内连续点击两次
     */
    @JvmStatic
    fun notRepeatClick(minClickDelayTime: Int): Boolean {
        val curClickTime = System.currentTimeMillis()
        if (curClickTime - lastClickTime >= minClickDelayTime) {
            lastClickTime = curClickTime
            return true
        }
        lastClickTime = curClickTime
        return false
    }

    @JvmStatic
    fun hideSoftInput(activity: Activity?) {
        try {
            hideSoftInput(activity?.window?.currentFocus)
        } catch (e: Exception) {
            Logger.e(e)
        }
    }

    @JvmStatic
    fun hideSoftInput(view: View?) {
        try {
            if (view == null) return
            val imm =
                    view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
            if (imm != null && imm.isActive) {
                imm.hideSoftInputFromWindow(view.windowToken, 0) //强制隐藏键盘
            }
        } catch (e: Exception) {
            Logger.e(e)
        }
    }

    @JvmStatic
    fun showSoftInput(editText: EditText?) {
        if (editText == null) return
        val imm = editText.context
                .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(editText, 0)
    }

}